package utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

public class Statistics {

	Map<String, List<Double>> fieldBaseSumList, minCapacityObjectList;

	public Statistics() {
		this.fieldBaseSumList = new HashMap<String, List<Double>>();
		this.minCapacityObjectList = new HashMap<String, List<Double>>();
	}

	public void addFieldBaseSum(String name, double fieldBaseSum) {
		if (fieldBaseSumList.get(name) == null) {
			List<Double> currentList = new ArrayList<Double>();
			currentList.add(fieldBaseSum);
			fieldBaseSumList.put(name, currentList);
		} else {
			List<Double> currentList = fieldBaseSumList.get(name);
			currentList.add(fieldBaseSum);
			fieldBaseSumList.put(name, currentList);
		}
	}

	public void addMinCapacityObject(String name, double minCapacityObject) {
		if (minCapacityObjectList.get(name) == null) {
			List<Double> currentList = new ArrayList<Double>();
			currentList.add(minCapacityObject);
			minCapacityObjectList.put(name, currentList);
		} else {
			List<Double> currentList = minCapacityObjectList.get(name);
			currentList.add(minCapacityObject);
			minCapacityObjectList.put(name, currentList);
		}
	}

	private double getMedian(Double[] doubles) {
		if (doubles.length < 3) {
			return doubles[0];
		}
		Arrays.sort(doubles);
		double median;
		if (doubles.length % 2 == 0)
			median = ((double) doubles[doubles.length / 2] + (double) doubles[doubles.length / 2 + 1]) / 2;
		else
			median = (double) doubles[doubles.length / 2];
		return median;
	}

	public void showStatistics() {
		System.out.println("\n");
		System.out.println("FieldBaseSumList: ");
		float sumFieldBase = 0;
		float sumMinCapacityObject = 0;
		SortedSet<String> keys = new TreeSet<String>(fieldBaseSumList.keySet());
		for (String key : keys) {
			List<Double> value = fieldBaseSumList.get(key);
			System.out.println("For " + key);
			for (double fieldBaseSum : value) {
				System.out.print(fieldBaseSum + ", ");
				sumFieldBase += fieldBaseSum;
				System.out.println();
				System.out.println("Minimum: " + Collections.min(value));
				System.out.println("Maximum: " + Collections.max(value));
				System.out.println("Median: "
						+ getMedian(value.toArray(new Double[value.size()])));
			}
			System.out.println("\n");
		}

		System.out.println("MinCapacityObjectList: ");
		keys = new TreeSet<String>(minCapacityObjectList.keySet());
		for (String key : keys) {
			List<Double> value = minCapacityObjectList.get(key);
			System.out.println("For " + key);
			for (double minCapacityObject : value) {
				System.out.print(minCapacityObject + ", ");
				sumMinCapacityObject += minCapacityObject;
				System.out.println();
				System.out.println("Minimum: " + Collections.min(value));
				System.out.println("Maximum: " + Collections.max(value));
				System.out.println("Median: "
						+ getMedian(value.toArray(new Double[value.size()])));
			}
			System.out.println("\n");
		}
		System.out.println("SUM OF ALL FIELD BASES: " + sumFieldBase);
		System.out
				.println("SUM OF ALL MIN CAPACITIES: " + sumMinCapacityObject);
	}
}
